//
//  ACRepeatCheck.m
//  AlarmClock
//
//  Created by kai ren on 01/11/2011.
//  Copyright 2011 Renkai. All rights reserved.
//

#import "ACRepeatCheck.h"


@implementation ACRepeatCheck


#pragma mark -
#pragma mark Initialization

- (id) initWithAlarm:(ACAlarm *)_alarm{
	if (self = [self initWithStyle:UITableViewStyleGrouped]) {
		alarm = _alarm;
		days = [[NSArray alloc] initWithObjects:
				NSLocalizedString(@"Every Monday", nil), 
				NSLocalizedString(@"Every Tuesday", nil), 
				NSLocalizedString(@"Every Wednesday", nil),
				NSLocalizedString(@"Every Thursday", nil),
				NSLocalizedString(@"Every Friday", nil),
				NSLocalizedString(@"Every Saturday", nil),
				NSLocalizedString(@"Every Sunday", nil),
				nil];
	}
	
	return self;
}

- (void)dealloc {
	[days release];
	
	[super dealloc];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];
	
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    
	cell.textLabel.text = [days objectAtIndex:indexPath.row];
	
	int repeat = alarm.repeat; 
	
	for (int i = 0; i < indexPath.row + 1; i++) {
		repeat = repeat / 10;
	}
	
	if ( repeat%10 ) {
		cell.accessoryType = UITableViewCellAccessoryCheckmark;
	} else {
		cell.accessoryType = UITableViewCellAccessoryNone;
	}
	
	if ( alarm.repeat%10 && indexPath.row == 6 ) {
		cell.accessoryType = UITableViewCellAccessoryCheckmark;
	}
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	
    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	
	int repeat = 1;
	
	if (indexPath.row != 6) {
		for (int i = 0; i < indexPath.row + 1; i++) {
			repeat = repeat * 10;
		}
	}	
	
	if (cell.accessoryType == UITableViewCellAccessoryNone) {
		cell.accessoryType = UITableViewCellAccessoryCheckmark;
		alarm.repeat = alarm.repeat + repeat;
	} else {
		cell.accessoryType = UITableViewCellAccessoryNone;
		alarm.repeat = alarm.repeat - repeat;
	}
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

@end

